import {defineStore} from 'pinia'
import {getToken, setToken, removeToken} from "@/utils/auth"
import {userInfo} from "@/api/my";

export const userStore = defineStore({
    id: 'user',
    state: () => {
        return {
            token: getToken() || '',
            userInfo: null,
        }
    },
    actions: {
        setUserInfo(data: any) {
            this.token = data.token
            setToken(data.token)
        },
        async getUserInfo() {
            if (this.userInfo == null) {
                const res = await userInfo();
                this.userInfo = res.data;
            }
            return this.userInfo;
        },
        logout() {
            this.token = ''
            this.userInfo = null
            removeToken()
        }
    }
})